package com.stu.entity.permission;

import java.beans.Transient;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import com.stu.entity.base.BaseEntity;

@Entity
@Table(name = "PERMISSION_USER")
public class PermissionUser extends BaseEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String loginCode;//登录账号
	private String loginPwd;//登录密码
	private List<PermissionRole> roles=new ArrayList<PermissionRole>();//角色集合
	private List<PermissionMenuRoot> menuRoots=new ArrayList<PermissionMenuRoot>();//跟菜单组集合
	private List<PermissionMenu> menus=new ArrayList<PermissionMenu>();//菜单集合
	private List<PermissionAuth> auths = new ArrayList<PermissionAuth>();//权限集合
	


	public void setAuths(List<PermissionAuth> auths) {
		this.auths = auths;
	}

	public PermissionUser() {
		super();
	}
	
	public PermissionUser(String uuid) {
		super(uuid);
	}
	
	public PermissionUser(String loginCode, String loginPwd, List<PermissionRole> roles, List<PermissionMenuRoot> menuRoots, List<PermissionMenu> menus) {
		super();
		this.loginCode = loginCode;
		this.loginPwd = loginPwd;
		this.roles = roles;
		this.menuRoots = menuRoots;
		this.menus = menus;
	}
	
	public void setLoginCode(String loginCode) {
		this.loginCode = loginCode;
	}
	public void setLoginPwd(String loginPwd) {
		this.loginPwd = loginPwd;
	}
	public void setRoles(List<PermissionRole> roles) {
		this.roles = roles;
	}
	public void setMenuRoots(List<PermissionMenuRoot> menuRoots) {
		this.menuRoots = menuRoots;
	}
	public void setMenus(List<PermissionMenu> menus) {
		this.menus = menus;
	}
	
	@Column(name = "logincode", length = 36, nullable = false)
	public String getLoginCode() {
		return loginCode;
	}
	
	@Column(name = "loginpwd", length = 30, nullable = false)
	public String getLoginPwd() {
		return loginPwd;
	}
	
	@ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
	@JoinTable(name = "PERMISSION_USER_ROLES", joinColumns = { @JoinColumn(name = "user_id", referencedColumnName = "uuid") }, inverseJoinColumns = { @JoinColumn(name = "role_id", referencedColumnName = "uuid") })
	public List<PermissionRole> getRoles() {
		return roles;
	}
	
	@ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
	@JoinTable(name = "PERMISSION_USER_MENUROOTS", joinColumns = { @JoinColumn(name = "user_id", referencedColumnName = "uuid") }, inverseJoinColumns = { @JoinColumn(name = "root_id", referencedColumnName = "uuid") })
	public List<PermissionMenuRoot> getMenuRoots() {
		return menuRoots;
	}
	
	@ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
	@JoinTable(name = "PERMISSION_USER_MENUS", joinColumns = { @JoinColumn(name = "user_id", referencedColumnName = "uuid") }, inverseJoinColumns = { @JoinColumn(name = "menu_id", referencedColumnName = "uuid") })
	public List<PermissionMenu> getMenus() {
		return menus;
	}
	
	@Transient
	public List<PermissionAuth> genAuths() {
		Iterator<PermissionRole> iterator = roles.iterator();
		while (iterator.hasNext()) {
			PermissionRole role = iterator.next();
			auths.addAll(role.getAuths());
		}
		return auths;
	}
	
	

	
}
